﻿using MediatR;
using RearLivestock.Beginning.Api.Application.Command.PrenatalCheckup;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.DeliveryTip
{
    /// <summary>
    /// 首次妊检提示
    /// </summary>
    public class SearchInitialPregnancyTestHandler : IRequestHandler<SearchInitialPregnancyTestCommand, Result>
    {
        /// <summary>
        /// 妊检仓储
        /// </summary>
        private readonly IBaseRepository<PrenatalCheckups> _prenatalCheckupsRepository;

        /// <summary>
        /// 猪只档案仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> _basicInformationRepository;

        /// <summary>
        /// 栋舍管理仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> _bedroomRepository;

        /// <summary>
        /// 栏位仓储
        /// </summary>
        private readonly IBaseRepository<Fields> _fieldsRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchInitialPregnancyTestHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prenatalCheckupsRepository"></param>
        /// <param name="basicInformationRepository"></param>
        /// <param name="bedroomRepository"></param>
        /// <param name="fieldsRepository"></param>
        /// <param name="logger"></param>
        public SearchInitialPregnancyTestHandler(IBaseRepository<PrenatalCheckups> prenatalCheckupsRepository,
                                                 IBaseRepository<BasicInformation> basicInformationRepository,
                                                 IBaseRepository<Bedrooms> bedroomRepository,
                                                 IBaseRepository<Fields> fieldsRepository,
                                                 ILogger<SearchInitialPregnancyTestHandler> logger)
        {
            _prenatalCheckupsRepository = prenatalCheckupsRepository;
            _basicInformationRepository = basicInformationRepository;
            _bedroomRepository = bedroomRepository;
            _fieldsRepository = fieldsRepository;
            _logger = logger;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SearchInitialPregnancyTestCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                //分页实例
                var page = new PageResultDto<PrenatalCheckupDtos>() { TotalCount = 0, PageCount = 0, List = new List<PrenatalCheckupDtos>() };
                //获取数据
                var query = (from a in await _prenatalCheckupsRepository.GetAll()
                             join b in await _basicInformationRepository.GetAll() on a.BasicInforId equals b.BasicInforId
                             join c in await _bedroomRepository.GetAll() on a.TurnBuilding equals c.BedroomId
                             join d in await _fieldsRepository.GetAll() on a.TurnColumn equals d.FieldId
                             select new PrenatalCheckupDtos()
                             {
                                 PrenatalCheckupId = a.PrenatalCheckupId,
                                 PregnancyTestDate = a.PregnancyTestDate,
                                 TurnBuilding = a.TurnBuilding,
                                 TurnColumn = a.TurnColumn,
                                 PregnancyTestResultId = a.PregnancyTestResultId,
                                 EarNumber = b.EarNumber,
                                 BedroomName = c.BedroomName,
                                 FieldName = d.FieldName,
                             });
                //总条数
                page.TotalCount = query.Count();
                //总页数
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
                //跳过几条 显示几条
                page.List = query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                //判断
                if (page.List.Count > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取首次种母妊检成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "获取首次种母妊检异常" + ex.Message;
                _logger.LogError("获取首次种母妊检异常" + ex.Message);
            }
            return result;
        }
    }
}
